#include "loopcomputerandgetnextcomputerdata.h"

#include <QDateTime>


LoopComputerAndGetNextComputerData::LoopComputerAndGetNextComputerData(QObject *parent)
    : QThread(parent)
    , m_isRunning(false){}

void LoopComputerAndGetNextComputerData::run()
{
    qDebug()<<"begin loop Thread loopThreadID:" << QThread::currentThread()<<"m_duration = "<<m_duration;
    //开始采集后，等待一个半的时间分辨率再进行computer，computer传入的时间范围为：currentDateTime-m_duration 到 currentDateTime。保障computer的请求时间囊括采集结束时间。
    QThread::sleep(3*m_duration/2);
    google::protobuf::Timestamp lastTime;
    lastTime.set_seconds(1000); //lastTime随便设个非0的值，在getNextComputerData之后就会更新此数
    while(m_isRunning){
        //computer
        lidar::ComputeParamsPackage computeParamsPackage;
        QDateTime currentDateTime = QDateTime::currentDateTime().addSecs(-m_duration);
        google::protobuf::Timestamp startTime, endTime;
        qint64 seconds = currentDateTime.toSecsSinceEpoch();
        qint64 msecs = currentDateTime.toMSecsSinceEpoch() % 1000;
        startTime.set_seconds(seconds);
        startTime.set_nanos(msecs * 1000000);

        currentDateTime = QDateTime::currentDateTime();
        seconds = currentDateTime.toSecsSinceEpoch();
        msecs = currentDateTime.toMSecsSinceEpoch() % 1000;
        endTime.set_seconds(seconds);
        endTime.set_nanos(msecs * 1000000);

        lidar::AlgrithmParams algrithmParams;

        computeParamsPackage.mutable_methodparams()->CopyFrom(algrithmParams);
        computeParamsPackage.mutable_dtfrom()->CopyFrom(startTime);
        computeParamsPackage.mutable_dtto()->CopyFrom(endTime);
        computeParamsPackage.mutable_id()->CopyFrom(m_guid);
        {
            qDebug()<<"-------------------------computer--------------------------";
            QMutexLocker<QMutex> locker(m_mutex.data());
            //调用compute失败
            if(!m_client->compute(computeParamsPackage)){
                //break;
            }
        }
        while(true){
            lidar::AlgrithmParamsPackage algrithmParamsPackage;
            algrithmParamsPackage.mutable_id()->CopyFrom(m_guid);
            algrithmParamsPackage.set_requestedmethods(0x0400);
            QSharedPointer<lidar::ComputedResult> response;
            {
                qDebug()<<"-------------------------getNextComputerData--------------------------";
                QMutexLocker<QMutex> locker(m_mutex.data());
                response = m_client->getNextComputerData(algrithmParamsPackage);
            }
            //如果!response说明调用getNextComputerData失败
            if(!response){
                break;
            }else{
                google::protobuf::Timestamp thisTime = response->acquiredat();
                if(thisTime.seconds() == 0 && thisTime.nanos() == 0 ){
                    qDebug()<<"acquiredat为空, response->acquiredat().seconds():"<<response->acquiredat().seconds() <<"tresponse->acquiredat().nanos()"<<response->acquiredat().nanos();
                    break;
                }
                if(thisTime.seconds() <= lastTime.seconds()){
                    qDebug()<<"查询到已经廓线的数据，进行下一轮lastTime";
                }else{
                    lastTime = thisTime;
                }
            }

            //获取消光系数的数据所在位置
            int metheadSize = response->methods_size();
            int extinctionCoefficientIndex = 0;
            for(int i = 0; i < metheadSize; i++){
                if(response->methods(i) == 0x0400){
                    extinctionCoefficientIndex = i;
                }
            }
            const std::string& temp = response->data(extinctionCoefficientIndex);
            qDebug()<<"解压前的数据长度： "<< temp.size();
            QByteArray compressDataElement = QByteArray::fromRawData(temp.data(), temp.size());
            QByteArray unCompressDataElement = qUncompress(compressDataElement);

            //extinctionCoefficientVec存放本次while循环获取到的消光系数
            QVector<float>  extinctionCoefficientVec;
            const float* floatPtr = reinterpret_cast<const float*>(unCompressDataElement.data());
            for(int i = 0; i < unCompressDataElement.size() / sizeof(float); i++){
                extinctionCoefficientVec.push_back(floatPtr[i]);
            }
            qDebug() << " size:" << extinctionCoefficientVec.size();
            for(int i = 0; i < 10; i++){
                qDebug() << " data:" << extinctionCoefficientVec[i];
            }
        }
        //间隔一个时间分辨率，进行一次computer，
        QThread::sleep(m_duration);
    }
    qDebug()<<"loop Thread over, ThreadID: " << QThread::currentThread();
}

void LoopComputerAndGetNextComputerData::StartThread()
{
    m_isRunning = true;
    start(); // 启动线程
}

void LoopComputerAndGetNextComputerData::StopThread()
{
    m_isRunning = false;
    wait(); // 等待线程结束
}
